package me.zhengjie.modules.logistics.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.kuaidi100.sdk.api.COrder;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.PrintReq;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.request.cloud.COrderCancelReq;
import com.kuaidi100.sdk.request.corder.COrderReq;
import com.kuaidi100.sdk.utils.SignUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.logistics.domain.req.LogisticsOrderCommitReq;
import me.zhengjie.modules.logistics.domain.req.LogisticsOrderReq;
import me.zhengjie.modules.logistics.domain.resp.LogisticsOrderResp;
import me.zhengjie.modules.logistics.service.LogisticsOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.NOT_LOGISTICS_ORDER;
import static me.zhengjie.modules.logistics.domain.constant.LogisticsOrderConstant.*;
import static net.dreamlu.mica.core.utils.WebUtil.getRequest;

@Service
@RequiredArgsConstructor
@Slf4j
public class LogisticsOrderServiceImpl implements LogisticsOrderService {


    @Override
    public LogisticsOrderResp queryOrder(LogisticsOrderReq req) throws Exception {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(req.getCom());
        queryTrackParam.setNum(req.getNum());
        queryTrackParam.setPhone(req.getPhone());
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(KUAIDI100_CUSTOMER);
        queryTrackReq.setSign(SignUtils.querySign(param ,KUAIDI100_KEY,KUAIDI100_CUSTOMER));

        IBaseClient baseClient = new QueryTrack();
        HttpResult execute = baseClient.execute(queryTrackReq);
        String body = execute.getBody();
        log.info("快递100返回信息为： {}",body);
        // 如果没有查询到快递订单
        if(body.contains("\"result\":false")){
            throw new BizException(NOT_LOGISTICS_ORDER.getCode(),NOT_LOGISTICS_ORDER.getMessage());
        }
        // 查询到订单，返回
        if(body.contains("ok")){
            LogisticsOrderResp resp = JSON.parseObject(body, LogisticsOrderResp.class);
            return resp;
        }
        return null;
    }

    @Override
    @Transactional
    public boolean submitOrder(LogisticsOrderCommitReq req) throws Exception {
        PrintReq printReq = new PrintReq();
        COrderReq cOrderReq = new COrderReq();
        cOrderReq.setKuaidicom(CompanyConstant.SF);
        cOrderReq.setSendManName(req.getSendManName());
        cOrderReq.setSendManMobile(req.getSendManMobile());
        cOrderReq.setSendManPrintAddr(req.getSendManPrintAddr());
        cOrderReq.setRecManName(req.getRecManName());
        cOrderReq.setRecManMobile(req.getRecManMobile());
        cOrderReq.setRecManPrintAddr(req.getRecManPrintAddr());
        cOrderReq.setCallBackUrl(CALL_BACK);
        cOrderReq.setRemark("测试下单，待会取消");

        String t = String.valueOf(System.currentTimeMillis());
        String param = new Gson().toJson(cOrderReq);

        printReq.setKey(KUAIDI100_KEY);
        printReq.setSign(SignUtils.printSign(param,t,KUAIDI100_KEY,SECRET));
        printReq.setT(t);
        printReq.setParam(param);
        printReq.setMethod(ApiInfoConstant.C_ORDER_METHOD);

        IBaseClient cOrder = new COrder();
        System.out.println(cOrder.execute(printReq));
        return true;
    }

    @Override
    @Transactional
    public boolean cancelOrder(LogisticsOrderReq req) throws Exception {
        PrintReq printReq = new PrintReq();
        COrderCancelReq cOrderCancelReq = new COrderCancelReq();
        cOrderCancelReq.setTaskId("FD80D2CD311E8F7A3D3AC28E515974AF");
        cOrderCancelReq.setOrderId("26732931");
        cOrderCancelReq.setCancelMsg("测试单取消");

        String t = String.valueOf(System.currentTimeMillis());
        String param = new Gson().toJson(cOrderCancelReq);

        printReq.setKey(KUAIDI100_KEY);
        printReq.setSign(SignUtils.printSign(param,t,KUAIDI100_KEY,SECRET));
        printReq.setT(t);
        printReq.setParam(param);
        printReq.setMethod(ApiInfoConstant.CANCEL_METHOD);

        IBaseClient bOrder = new COrder();
        System.out.println(bOrder.execute(printReq));
        return true;
    }
}
